<?php
include_once(dirname(__FILE__)."/../inc/common.php");
$method = isset($_GET["method"])?$_GET["method"]:"";

$flowClass = new tally_flow();

if($method=="listQry") {
    // 所选年每月收入
    $year = isset($_POST["year"])?$_POST["year"]:null;
    $tagIds = isset($_POST["tagId"])?trim($_POST["tagId"], ","):"";
    $categoryId = isset($_POST["categoryId"])?$_POST["categoryId"]:null;

    $incomeList = array();

    for($month=1;$month<=12;$month++){
        $nextMonthYear = $year;
        $nextMonth = $month+1;
        if($nextMonth>12){
            $nextMonth = 1;
            $nextMonthYear++;
        }
        $startTime = strtotime($year."-".$month."-01");
        $endTime = strtotime($nextMonthYear."-".$nextMonth."-01");

        $rows = $flowClass->getCountByTimeRangeGroupByType($userId,
            tally_flow::$INOUT_INCOMME, $startTime, $endTime, $tagIds, $categoryId);

        foreach ($rows as $row) {
            $flowType = $row["flowType"];
            if(!isset($incomeList[$flowType])){
                $incomeList[$flowType] = array(
                    "name" => $row["name"],
                    "data" => array()
                );
            }
            $incomeList[$flowType]["data"][$month] = $row["sumIn"];
        }
    }
    ?>
    <table id="listtb" class="listtable" cellpadding="0" cellspacing="1">

        <tr>
            <th width="16%">&nbsp;</th>

            <th width="7%">1月</th>

            <th width="7%">2月</th>

            <th width="7%">3月</th>

            <th width="7%">4月</th>

            <th width="7%">5月</th>

            <th width="7%">6月</th>

            <th width="7%">7月</th>

            <th width="7%">8月</th>

            <th width="7%">9月</th>

            <th width="7%">10月</th>

            <th width="7%">11月</th>

            <th width="7%">12月</th>

            <th width="10%">合计</th>
        </tr>


        <?php
        foreach($incomeList as $row){
            $ratioList = array();
            $sum = 0;
        ?>
        <tr>
            <td><?php echo $row["name"]?> </td>
            <?php
            for($month=1;$month<=12;$month++){
                $v = isset($row["data"][$month])?$row["data"][$month]:0;
            ?>
                <TD><?php echo number_format(abs($v), 2, ".", "");?></TD>
            <?php
                $sum += abs($v);
                if($month>1 && $v>0){
                    $lastMonthVal = isset($row["data"][$month-1])?$row["data"][$month-1]:0;
                    $ratioList[] = $lastMonthVal>0?number_format(($v-$lastMonthVal)*100/$lastMonthVal, 2, ".", "")."%":"100%";
                }else{
                    $ratioList[] = "-";
                }
            }
            ?>
            <TD><?php echo number_format(abs($sum), 2, ".", "");?></TD>
        </tr>

        <tr class="dis">
            <td>环比</td>
            <?php
            foreach($ratioList as $v){
                ?>
                <TD><?php echo $v;?></TD>
                <?php
            }
            ?>
            <TD>-</TD>
        </tr>
        <?php
        }
        ?>

        <tr>
            <td colspan="13"  class="to_left">
                (注：按最新汇率,以人民币计)
            </td>
        </tr>
    </table>
    <?php
}else if($method=="pole") {// 所选年每月收入
    $year = isset($_POST["year"])?$_POST["year"]:null;
    $tagIds = isset($_POST["tagId"])?trim($_POST["tagId"], ","):"";
    $categoryId = isset($_POST["categoryId"])?$_POST["categoryId"]:null;

    $categories = array();
    $incomeList = array();

    for($month=1;$month<=12;$month++){
        $nextMonthYear = $year;
        $nextMonth = $month+1;
        if($nextMonth>12){
            $nextMonth = 1;
            $nextMonthYear++;
        }
        $startTime = strtotime($year."-".$month."-01");
        $endTime = strtotime($nextMonthYear."-".$nextMonth."-01");

        $categories[] = date("m月", $startTime);
        $rows = $flowClass->getCountByTimeRangeGroupByType($userId,
            tally_flow::$INOUT_INCOMME, $startTime, $endTime, $tagIds, $categoryId);

        foreach ($rows as $row) {
            $flowType = $row["flowType"];
            if(!isset($incomeList[$flowType])){
                $incomeList[$flowType] = array(
                    "name" => $row["name"],
                    "data" => array()
                );
            }
            $incomeList[$flowType]["data"][$month] = $row["sumIn"];
        }
    }

    $series = array();
    foreach ($incomeList as $row){
        $data = array();
        for($month=1;$month<=12;$month++){
            $data[] = isset($row["data"][$month])?$row["data"][$month]+0:0;
        }

        $series[] = array(
            "name" => $row["name"],
            "data" => $data
        );
    }

    echo json_encode(array(
        "categories" => $categories,
        "series" => $series,
    ));
}else{
    $flowClass = new tally_flow();
    $flowList = $flowClass->findFlowByUserId($userId, 0, 0, "", "",
        "", "", 0, 0, "tradeTimeAsc", 0, 1);
    $firstTradeYear = date("Y", strtotime($flowList[0]["tradeTime"]));
    $currentYear = date("Y", time());
    ?>
    <!-- begin:导航条 -->
    <div class="titlebar">
        <h3>收入趋势统计</h3>
    </div>

    <div class="tab_wrapper">
        <div class="body">
            <!-- begin:搜索区 -->
            <form id="schForm" name="schForm" method="post">
                <div class="searchbar" id="searchbar" style="line-height: 150%; display: block;">
                    <div class="base_condition">
                        <table>
                            <tr>
                                <TD><label for="year">统计年度:</label>
                                    <SELECT name="year" id="year">

                                        <?php
                                        for($i=$firstTradeYear;$i<=$currentYear;$i++) {
                                            ?>
                                            <option value="<?php echo $i;?>" <?php echo $i==$currentYear?"selected":""?>>
                                                <?php echo $i;?>
                                            </option>
                                            <?php
                                        }
                                        ?>

                                    </SELECT>
                                </TD>
                                <td><input id="searchBtn" class="button" type="button" value="查询" onclick="IncomeAnnulusReport.search();Report.clearData();Report.searchChart();" /></td>
                            </tr>
                        </table>
                    </div>

                    <TABLE class="complex_condition">
                        <TR>
                            <td>
                                <label for="tags">
                                    标签:
                                </label>
                                <input type="text" class="text rd" id="tags" name="tags"
                                       readonly="readonly" onClick="CM.clearValue(this);">
                                <input type="hidden" value="" name="tagId" id="tagId"/>
                                <a title="选择项目" href="#"
                                   onclick="TagPicker.open(this,'1');return false;"><img src="/images/icons/select.gif" align="absmiddle" />
                                </a>
                            </td>

                            <td>
                                <label for="categoryName">
                                    收支类别:
                                </label>
                                <input type="hidden" id="categoryId" name="categoryId" />
                                <input type="text" class="text rd" id="categoryName" name="categoryName" readonly="readonly" onClick="CM.clearValue(this);" >
                                <a title="收支类别" href="#"
                                   onclick="CategoryPicker.open(this,'');return false;"><img
                                        src="/images/icons/select.gif" align="absmiddle" />
                                </a>
                            </td>

                        </tr>


                    </TABLE>

                </div>
            </form>
            <!-- end:搜索区 -->

            <div class="report_bar">
                <span>数据列表</span>
            </div>

            <div id="dataView" class="data">
                <div align="center">
                    <img align="absmiddle" src='/images/common/loading1.gif'>
                </div>
            </div>


            <div class="report_bar">
                <span>数据图表</span>
            </div>

            <div id="chartView" class="chartView">
                <div  align="center"><img align="absmiddle" src='/images/common/loading1.gif'></div>
            </div>
            <!-- end:---------主数据区 -->
        </div>
    </div>
    <?php
}
?>
